package com.sf.osms.dep.processor.handler;

import org.apache.commons.lang3.StringUtils;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.processor.handler.convert.BasicInfoConverter;
import com.sf.osms.dep.processor.handler.convert.CommonConverter;
import com.sf.osms.dep.processor.handler.convert.ConverterChain;
import com.sf.osms.dep.processor.handler.convert.EventCodeConverter;
import com.sf.osms.dep.processor.util.RouterConstants;

public class TextEventHandler extends AbstractEventHandler<Object>{
	
	/**
	 * 进行行错误记录的重组，用于存放到DB中错误记录的原始数据
	 */
	@Override
	protected String getRecordStr(Object record,Agent agentInfo) {
		if(record instanceof String[]){
			String[] records = (String[])record;
			String errorLine = "";
			String separator =agentInfo.getAgentFileMap().getColumnSep();
			if(StringUtils.isBlank(separator)){
				separator = RouterConstants.AGENT_FILE_COLUMN_SEPARTOR;
			}
			
			for (String string : records) {
				
				errorLine = errorLine + separator + string;
			}
			errorLine = errorLine.substring(errorLine.indexOf(separator)+1);
			
			return errorLine;
		}else{
			return record.toString();
		}
	}

	/**
	 * 注册Converter来进行处理逻辑的组装
	 */
	@Override
	public void registerConverters(ConverterChain converterChain) {
		converterChain.addConverter(new BasicInfoConverter());
		converterChain.addConverter(new CommonConverter());
		converterChain.addConverter(new EventCodeConverter());
	}
	
}
